package com.itheima.servlet;

import com.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.http.HttpClient;

/**
 * @Author: 祺诺
 * @Name; xqf
 * @Date: 2020/12/26 20:38
 */
@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String score = req.getParameter("score");
        Student student = new Student(name, Integer.parseInt(age), Integer.parseInt(score));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\stu.txt",true));
        bw.write(student.getName()+","+student.getAge()+","+student.getScore());
        bw.newLine();
        bw.close();
        resp.getWriter().write("添加成功两秒后返回首页");
        resp.setHeader("Refresh","2;URL="+req.getContextPath()+"/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);
    }
}
